﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using KZ.Exception;
using Process.BLL;
using Process.Entities;
using Process;

namespace ProviderServices
{
    /// <summary>
    /// Summary description for ServiceForBusiness
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ServiceForBusiness : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        public string WSLogin(string username, string password)
        {
            try
            {
                string sID = HttpContext.Current.Session.SessionID;
                if (PartnerBLL.ValidateLogin(sID, username, password))
                {
                    return sID;
                }
                throw new BusinessLogicException("Có lỗi khi đăng nhập hệ thống.");
            }
            catch
            {
                throw;
            }
        }

        [WebMethod]
        public DataTable Category(string parentCategoryID)
        {
            try
            {
                DataTable dt = CategoryBLL.GetCategoryListForCMB(parentCategoryID);
                dt.TableName = "Category";
                return dt;
            }
            catch
            {
                throw;
            }
        }

        [WebMethod]
        public DataTable Product(string categoryID)
        {
            try
            {
                DataTable dt = ProductBLL.GetProductList(categoryID);
                dt.TableName = "Product";
                return dt;
            }
            catch
            {
                throw;
            }
        }

        [WebMethod(EnableSession = true)]
        public string RegisterTrans(string sID, string transID, string productID, int minQuantity, int nextTransQuantity,
            DateTime beginTransDate, DateTime endTransDate)
        {
            try
            {
                string partnerID = SessionBLL.ValidateSession(sID);

                TransactionBLL transBLL = new TransactionBLL();
                if (transBLL.InsertTransaction(transID, partnerID, productID, minQuantity, nextTransQuantity,
                    beginTransDate, endTransDate))
                {
                    return "0";
                }
                return "-1";
            }
            catch
            {
                return "-2";
            }
        }

        [WebMethod(EnableSession = true)]
        public string EndUpdateInventory(string sID, string transID, string msg)
        {
            try
            {
                string partnerID = SessionBLL.ValidateSession(sID);

                OrderBLL orderBLL = new OrderBLL();
                if (orderBLL.UpdateOrderStatus(transID, OrderStatus.Done))
                {
                    return "0";
                }
                return "-1";
            }
            catch
            {
                return "-2";
            }
        }
    }
}
